package top.infopub.mgr.om.service.impl;


import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.om.dao.PaymentApplyAttDao;
import top.infopub.mgr.om.dao.PaymentApplyDao;
import top.infopub.mgr.om.dao.PaymentApplyDetailDao;
import top.infopub.mgr.om.domain.PaymentApplyDomain;
import top.infopub.mgr.om.service.PaymentApplyService;
import top.infopub.mgr.om.vo.PaymentApplyVo;
import top.infopub.model.om.PaymentApply;
import top.infopub.model.om.PaymentApplyAtt;
import top.infopub.model.om.PaymentApplyDetail;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class PaymentApplyServiceImpl extends GenericServiceImpl<PaymentApply, String> implements PaymentApplyService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private PaymentApplyDao paymentApplyDao;

    @Autowired
    private PaymentApplyDetailDao paymentApplyDetailDao;

    @Autowired
    private PaymentApplyAttDao paymentApplyAttDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<PaymentApply, String> getDao() {
        return paymentApplyDao;
    }

    @Override
    public PaymentApplyDomain selectById(String id) {
        return paymentApplyDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPaymentApply(PaymentApply paymentApply, List<PaymentApplyDetail> items,
                                List<PaymentApplyAtt> fileAtts) {

        //插入付款申请主表记录
        paymentApplyDao.insertById(paymentApply);

        //插入批量付款明细
        if (CollectionUtils.isNotEmpty(items)) {
            paymentApplyDetailDao.insertByBatch(items);
        }

        //批量插入附件
        if (CollectionUtils.isNotEmpty(fileAtts)) {
            paymentApplyAttDao.insertByBatch(fileAtts);
        }
        
        String srcBizTable = "OM_PAYMENT_APPLY";
        String branchCode_xm = "from_项目发起";
        String branchCode_super = "from_总部发起";
        String hrOrgId = paymentApply.getHrOrgId();// 申请公司
        // 公司类型 1=总部公司 2=区域公司 3=项目公司
        String type = orgInfoService.selectByPrimaryKey(hrOrgId).getTypes();
        String templateId = "";
        // 总部发起 
        if ("1".equals(type)) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_super);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_xm);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId,srcBizTable, paymentApply.getId(),
            paymentApply.getProcessId(), null);
    }

    @Override
    public Page<PaymentApplyDomain> selectMyCheckByDomainWhere(Page<PaymentApplyDomain> page,
                                                               PaymentApplyVo paymentApplyVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PaymentApplyDomain>)paymentApplyDao.selectMyCheckByDomainWhere(paymentApplyVo);
    }

    @Override
    public Page<PaymentApplyDomain> selectByDomainWhere(Page<PaymentApplyDomain> page,
                                                        PaymentApplyVo paymentApplyVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PaymentApplyDomain>)paymentApplyDao.selectByDomainWhere(paymentApplyVo);
    }

}